package com.bailugansan.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author BailuGansan
 */

// 数据库基础公共类
public class DbUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    // 静态代码块，类加载的时候在连接阶段就初始化了
    static{
        Properties properties = new Properties();
        // 获取类加载器
        ClassLoader classLoader = DbUtil.class.getClassLoader();
        // 获取数据库配置文件的输入流
        InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");

        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    // 获取数据库连接
    public static Connection getConn(){
        Connection connection = null;
        try {
            // 加载驱动
            Class.forName(driver);
            // 获取连接
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 查询公共方法
    // 参数全部提取出来方便统一关闭
    public static ResultSet execute(Connection conn, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement){
        try {
            // 获取预编语句
            preparedStatement = conn.prepareStatement(sql);
            // 填充占位符
            for (int i = 0; i < params.length; i++) {
                // 占位符从 1 开始
                preparedStatement.setObject(i+1, params[i]);
            }
            // 执行
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    // 增删改公共方法
    public static int update(Connection conn, String sql, Object[] params, int result, PreparedStatement preparedStatement){
        try {
            preparedStatement = conn.prepareStatement(sql);
            // 填充占位符
            for (int i = 0; i < params.length; i++) {
                // 占位符从 1 开始
                preparedStatement.setObject(i+1, params[i]);
            }
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    // 关闭连接,释放资源
    public static boolean closeConn(Connection conn, PreparedStatement prep, ResultSet resu){
        boolean flag = true;
        try {
            if(conn != null){
                conn.close();
                // GC 回收
                conn = null;
            }
            if(prep != null){
                prep.close();
                prep = null;
            }
            if(resu != null){
                resu.close();
                resu = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}
